package cn.com.bluemoon.daps.model.controller;


import cn.com.bluemoon.common.KeyAnnotationClz;
import cn.com.bluemoon.common.UserPermission;
import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.datascope.log.OperateLog;
import cn.com.bluemoon.daps.common.datascope.log.enums.ModuleObject;
import cn.com.bluemoon.daps.common.datascope.log.enums.OperateType;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.model.entity.DapDataModelGroup;
import cn.com.bluemoon.daps.model.service.DapDataModelGroupService;
import cn.com.bluemoon.daps.model.vo.group.in.GroupTableSearchVo;
import cn.com.bluemoon.daps.model.vo.group.in.TableAddGroupVo;
import cn.com.bluemoon.daps.model.vo.group.out.GroupModelTableVo;
import cn.com.bluemoon.daps.model.vo.group.out.ModelGroupDto;
import cn.com.bluemoon.daps.model.vo.group.out.ModelLeftGroupOut;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;

/**
 * <p>
 * 模型分组表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-02-02
 */
@Api(tags = "模型分组")
@RestController
@Slf4j
@RequestMapping("/modelGroup")
@BmPermission(value = "模型分组功能")
@Validated
public class DapDataModelGroupController {

    @Autowired
    private DapDataModelGroupService groupService;

    @PostMapping("/list")
    @ApiOperation("分组管理-左树查询")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = ModelLeftGroupOut.class)})
    @BmPermission(value = "model:table:group", name = "分组管理-左树查询")
    public ResultBean<ModelLeftGroupOut> queryList(@ApiParam("模型id") @RequestParam("modelId") String modelId) {
        String keyword = ""; // 预留，目前业务不需要
        ModelLeftGroupOut modelLeftGroupOut = groupService.modelLeftGroup(modelId, keyword, true);
        return ResultBean.ok(modelLeftGroupOut);
    }


    @PostMapping("/add")
    @ApiOperation("分组管理-添加")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = DapDataModelGroup.class)})
    @UserPermission.SingleRow(keyFormClz = KeyAnnotationClz.RequestParam, key = "modelId", errorMsg = "只允许模型授权者进行新增分组功能")
    @BmPermission(value = "model:group:add", name = "分组管理-添加")
    @OperateLog(type = OperateType.INSERT, moduleName = ModuleObject.DATA_MODEL)
    public ResultBean<DapDataModelGroup> add(@ApiParam("模型id") @RequestParam("modelId") String modelId,
                                             @ApiParam("组路径") @RequestParam(value = "groupRootId", required = false) String groupRootId,
                                             @ApiParam("组名") @RequestParam("groupName")
                                             @Size(min = 1, max = 64, message = "组名不大于{max}，不小于{min}") String groupName,
                                             @ApiParam("排序") @RequestParam("groupRank")
                                             @Min(value = 1, message = "最小排序号为1")
                                             @Max(value = 255, message = "最大排序号为255") Integer groupRank) {
        DapDataModelGroup newAddGroup = groupService.addGroupByGroupPid(modelId, groupRootId, groupName, groupRank);
        return ResultBean.ok(newAddGroup);
    }

    @GetMapping("/get")
    @ApiOperation("分组管理-获取当前节点+父节点信息")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = ModelGroupDto.class)})
    @BmPermission(value = "model:group:get", name = "分组管理-查看")
    @UserPermission.SingleRow(keyFormClz = KeyAnnotationClz.RequestParam, key = "modelId", errorMsg = "只允许模型授权者进行查看分组功能")
    public ResultBean<ModelGroupDto> get(@ApiParam("组id") @RequestParam("groupId") String groupId,
                                         @ApiParam("模型id") @RequestParam("modelId") String modelId) {
        groupService.checkGroupAndModel(groupId, modelId);
        ModelGroupDto modelGroupDto = groupService.getCurrentGroupAndPreGroup(groupId);
        return ResultBean.ok(modelGroupDto);
    }

    @PutMapping("/edit")
    @ApiOperation("分组管理-编辑")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = DapDataModelGroup.class)})
    @UserPermission.SingleRow(keyFormClz = KeyAnnotationClz.RequestParam, key = "modelId", errorMsg = "只允许模型授权者进行编辑分组功能")
    @BmPermission(value = "model:group:edit", name = "分组管理-编辑")
    @OperateLog(type = OperateType.UPDATE, moduleName = ModuleObject.DATA_MODEL)
    public ResultBean<DapDataModelGroup> edit(@ApiParam("模型id") @RequestParam("modelId") String modelId,
                                              @ApiParam("组id") @RequestParam("groupId") String groupId,
                                              @ApiParam("组名") @RequestParam("groupName")
                                              @Size(min = 1, max = 64, message = "组名不大于{max}，不小于{min}") String groupName,
                                              @ApiParam("排序") @RequestParam("groupRank")
                                              @Min(value = 1, message = "最小排序号为1")
                                              @Max(value = 255, message = "最大排序号为255") Integer groupRank) {
        groupService.checkGroupAndModel(groupId, modelId);
        DapDataModelGroup newAddGroup = groupService.editGroupById(groupId, groupName, groupRank);
        return ResultBean.ok(newAddGroup);
    }

    @DeleteMapping("/delete")
    @ApiOperation("分组管理-删除")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = String.class)})
    @UserPermission.SingleRow(keyFormClz = KeyAnnotationClz.RequestParam, key = "modelId", errorMsg = "只允许模型授权者进行删除分组功能")
    @BmPermission(value = "model:group:delete", name = "分组管理-删除")
    @OperateLog(type = OperateType.DELETE, moduleName = ModuleObject.DATA_MODEL)
    public ResultBean<String> delete(@ApiParam("模型id") @RequestParam("modelId") String modelId,
                                     @ApiParam("组id") @RequestParam("groupId") String groupId) {
        groupService.checkGroupAndModel(groupId, modelId);
        groupService.delGroupById(groupId);
        return ResultBean.ok();
    }

    @PostMapping("/group/tables")
    @ApiOperation("分组管理-点击节点获取表数据/查询分组下的表数据")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = GroupModelTableVo.class)})
    @BmPermission(value = "model:table:group", name = "分组管理-表数据")
    public ResultBean<IPage<GroupModelTableVo>> queryTablesByGroup(@RequestBody GroupTableSearchVo<?> searchVo) {
        IPage<GroupModelTableVo> page = groupService.pageModelTablesByGroupId(searchVo);
        return ResultBean.page(page);
    }

    @PostMapping("/tables/group")
    @ApiOperation("分组管理-加入组")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = String.class)})
    @UserPermission.SingleRow(keyFormClz = KeyAnnotationClz.RequestBody, key = "modelId", errorMsg = "只允许模型授权者进行加入组功能")
    @BmPermission(value = "model:group:join", name = "分组管理-加入组")
    @OperateLog(type = OperateType.INSERT, moduleName = ModuleObject.DATA_MODEL)
    public ResultBean<String> tablesAddGroup(@Valid @RequestBody TableAddGroupVo tableAddGroupVo) {
        groupService.checkGroupAndModel(tableAddGroupVo.getGroupId(), tableAddGroupVo.getModelId());
        groupService.addGroupRelationToTables(tableAddGroupVo.getTableIds(), tableAddGroupVo.getGroupId());
        return ResultBean.ok();
    }

    @DeleteMapping("/tables/group/delete")
    @ApiOperation("分组管理-删除表")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = String.class)})
    @UserPermission.SingleRow(keyFormClz = KeyAnnotationClz.RequestBody, key = "modelId", errorMsg = "只允许模型授权者进行删除表功能")
    @BmPermission(value = "model:group:delTables", name = "分组管理-删除表", ignore = true)
    @OperateLog(type = OperateType.DELETE, moduleName = ModuleObject.DATA_MODEL)
    public ResultBean<String> batchDelTableOnGroup(@Valid @RequestBody TableAddGroupVo tableAddGroupVo) {
        groupService.checkGroupAndModel(tableAddGroupVo.getGroupId(), tableAddGroupVo.getModelId());
        groupService.batchDeleteTables(tableAddGroupVo.getTableIds(), tableAddGroupVo.getGroupId(), tableAddGroupVo.getModelId());
        return ResultBean.ok();
    }

    @PostMapping("/model/groups")
    @ApiOperation("模型设计-左树查询")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = ModelLeftGroupOut.class)})
    @BmPermission(value = "model:table:group", name = "模型设计-左树查询")
    public ResultBean<ModelLeftGroupOut> modelGroups(@ApiParam("关键字") @RequestParam(value = "keyword", required = false)
                                                     @Size(max = 255, message = "关键字不大于{max}") String keyword,
                                                     @ApiParam("模型id") @RequestParam("modelId") String modelId) {
        ModelLeftGroupOut modelLeftGroupOut = groupService.modelLeftGroupAndCommonModel(modelId, keyword);
        return ResultBean.ok(modelLeftGroupOut);
    }

}
